<?php

	require('./inc/common.php');
	$param = getParam($_GET);
	
	//缩放等级大于16,显示机构，否则显示板块
	if ($param['zoom'] > 15) {
		$data = showJiGou($param);
	}else {
		$data = showBlock($param);
	}
	
	//输出内容
    if (false != $data) {
    	header('Content-type: application/json');
		echo json_encode($data);
	}else {
		echo 'false';
	}
	
	//===================   函数定义   ========================
	
	/*
	 * 获取机构坐标数据
	 * 参数: $_GET[p4] 屏幕可见范围的最大纬度
	         $_GET[p4] 屏幕可见范围的最小纬度
	
	 * 返回: 数组 或 false
	*/
	function showJiGou($param)
	{
		$andPrice = $param['price'] == 0 ? '' : ' AND `jigou_price` IN('.$param['price'].')';
		$andFeature = $param['model'] == 0 ? '' : ' AND `jigou_cats` IN('.$param['model'].')';
		$andAge = $param['age'] == 0 ? '' : ' AND `jigou_age` IN ('.$param['age'].')';
		$andTime = $param['time'] == 0 ? '' : ' AND `jigou_period` IN ('.$param['time'].')';

		$sql = "SELECT *
	           FROM  zj_jigou
	           WHERE `jigou_map_lat` > ".$param['lat_min']."
	               AND `jigou_map_lat` < ".$param['lat_max']."
	               AND `jigou_map_lng` > ".$param['lng_min']."
	               AND `jigou_map_lng` < ".$param['lng_max']."
				   $andPrice
				   $andFeature
				   $andAge
				   $andTime
				ORDER BY jigou_id ASC
				";

	    $result = mysql_query($sql);

	    if (!$result) {
	        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	        return false;
	    }

		$jigouData = false;
	    while ($row = mysql_fetch_assoc($result)) {
			$jigouData[$row["jigou_id"]] = array(
				0 => $row["jigou_title"],
				1 => "",
				2 => $row["jigou_map_lat"],
				3 => $row["jigou_map_lng"],
				4 => $row["jigou_title"],
				5 => 0,
			);
	    }
	    mysql_free_result($result);
	
		return $jigouData;
	}
	
	function showBlock($param){
		$andPrice = $param['price'] == 0 ? '' : ' AND `jigou_price` IN('.$param['price'].')';
		$andFeature = $param['model'] == 0 ? '' : ' AND `jigou_cats` IN('.$param['model'].')';
		$andAge = $param['age'] == 0 ? '' : ' AND `jigou_age` IN ('.$param['age'].')';
		$andTime = $param['time'] == 0 ? '' : ' AND `jigou_period` IN ('.$param['time'].')';
		
		$sql = "SELECT t.*, j.*
		FROM `zj_type` t
		LEFT JOIN `zj_jigou` j ON j.jigou_area = t.type_id
		WHERE t.type_id >=12 && t.type_id <=29
		";

	    $result = mysql_query($sql);

	    if (!$result) {
	        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	        return false;
	    }

		$block = false;
	    while ($row = mysql_fetch_assoc($result)) {
			$block[$row['type_name']] = $row;
			
			// $block[$row["jigou_id"]] = array(
			// 	0 => '',
			// 	1 => $row["jigou_id"] .'个',		//数量
			// 	2 => $row["jigou_map_lat"],
			// 	3 => $row["jigou_map_lng"],
			// 	4 => $row["jigou_title"],
			// 	5 => 0,
			// );
	    }
	    mysql_free_result($result);		
		echo '<pre>';
		print_r ($block);
		echo '</pre>';
		die;
		return $block;
	}